package com.example.javabasic.thread.suanfa;

/**
 * @description 顺序执行
 * @Author Lin FuYuan
 * @date 2020/10/16
 */
public class Foo {
    private Boolean firstBool = false;
    private Boolean secondBool = false;
    private final static Object lock = new Object();
    public Foo() {


    }

    public void first(Runnable printFirst) throws InterruptedException {
        synchronized (lock){
            // printFirst.run() outputs "first". Do not change or remove this line.
            printFirst.run();
            firstBool = true;
            lock.notifyAll();
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {
        synchronized (lock){
            while(!firstBool){
                lock.wait();
            }
            // printSecond.run() outputs "second". Do not change or remove this line.
            printSecond.run();
            secondBool = true;
            lock.notifyAll();
        }
    }

    public void third(Runnable printThird) throws InterruptedException {
        synchronized (lock){
            while(!secondBool){
                lock.wait();
            }
            // printThird.run() outputs "third". Do not change or remove this line.
            printThird.run();

        }
    }
}
